/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaPrincipal.java
 *
 * Created on 2/Abr/2012, 14:48:58
 */
package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import sokoban.EstadoSokoban;

/**
 *
 * @author Leonardo Lino
 */
public class JanelaPrincipal extends javax.swing.JFrame {

    /** Creates new form JanelaPrincipal */
    public JanelaPrincipal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        areaDesenho = new main.AreaDesenho();
        escolhaPuzzle = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout areaDesenhoLayout = new javax.swing.GroupLayout(areaDesenho);
        areaDesenho.setLayout(areaDesenhoLayout);
        areaDesenhoLayout.setHorizontalGroup(
            areaDesenhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 796, Short.MAX_VALUE)
        );
        areaDesenhoLayout.setVerticalGroup(
            areaDesenhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        escolhaPuzzle.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "soko001.txt", "soko002.txt", "soko003.txt", "soko004.txt" }));
        escolhaPuzzle.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                escolhaPuzzleItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(areaDesenho, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(escolhaPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(escolhaPuzzle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(184, 184, 184)
                .addComponent(areaDesenho, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void escolhaPuzzleItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_escolhaPuzzleItemStateChanged
        areaDesenho.carregaPuzzle(evt.getItem().toString());
        //System.out.println(evt.getItem().toString());
        
    }//GEN-LAST:event_escolhaPuzzleItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JanelaPrincipal().setVisible(true);
            }
        });
    }
    
        public static char[][] lerFicheiroProblema(File f) throws Exception{
        ArrayList<char[]> listaLinhas = new ArrayList<char[]>();
        BufferedReader br = new BufferedReader(new FileReader(f));

        String linha = br.readLine();
        while(linha != null){
            listaLinhas.add(linha.toCharArray());
            linha = br.readLine();
        }

        return (char[][]) listaLinhas.toArray(new char[0][0]);
    }
    
    public static boolean eValido(char[][] tabela){
        int numAgentes, numCaixotes, numObjetivos;
        numAgentes = numCaixotes = numObjetivos = 0;
        for (int i = 0; i < tabela.length; i++) {
            if(tabela[i].length != tabela[0].length){
                return false;
            }
            for (int j = 0; j < tabela[0].length; j++) {
                switch(tabela[i][j]){
                    case 'P':
                    case 'V':
                        break;
                    case 'O':
                        numObjetivos++;
                        break;
                    case 'C':
                        numCaixotes++;
                        break;
                    case 'X':
                        numObjetivos++;
                        numCaixotes++;
                        break;
                    case 'A':
                        numAgentes++;
                        break;
                    default:
                        return false;
                }
            } 
        }
        
        if(numAgentes != 1 || numCaixotes != numObjetivos || numCaixotes == 0){
            return false;
        }
        
        return true;
    }
    
    public void carregarProblema(){
        char[][] chars = null;
        File fich = null;
        JFileChooser jf = new JFileChooser();
        int resposta = jf.showOpenDialog(this);
        if(resposta == JFileChooser.APPROVE_OPTION){
            fich = jf.getSelectedFile();
        }
        
        try {
            chars = lerFicheiroProblema(fich);
        } catch (Exception ex) {
            System.out.println("Não foi possível ler o ficheiro: " + ex);
        }
        
        if(eValido(chars)){
            System.out.println("Ficheiro válido! :)");
            System.out.println(new EstadoSokoban(chars));
        } else{
            System.out.println("Ficheiro inválido... :(");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public main.AreaDesenho areaDesenho;
    private javax.swing.JComboBox escolhaPuzzle;
    // End of variables declaration//GEN-END:variables
}
